package ldkxingzhe.ldkxingzhedemo.touchevent;

import android.os.Bundle;
import android.support.v4.view.MotionEventCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.widget.LinearLayout;
import android.widget.TextView;

import ldkxingzhe.ldkxingzhedemo.utils.LogDemo;

/**
 * Created by liangdengke on 2015/9/6.
 */
public class DragAndScalingActivity extends AppCompatActivity{

    private final String TAG = DragAndScalingActivity.class.getSimpleName();

    private final int INVALID_POINTER_ID = -1;
    private int mActionID = INVALID_POINTER_ID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout layout = new LinearLayout(this);
        TextView textView = new TextView(this);
        textView.setText("test dragging and scaling");
        layout.addView(textView);
        setContentView(layout);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        final int action = MotionEventCompat.getActionMasked(event);
        final int index = event.getActionIndex();
        switch (action){
            case MotionEvent.ACTION_DOWN:
                LogDemo.v(TAG,"action_Down, index = " + index);
                mActionID = event.getPointerId(index);
                break;
            case MotionEvent.ACTION_MOVE:
                LogDemo.v(TAG,"action_move, index = " + index);
                break;
            case MotionEventCompat.ACTION_POINTER_DOWN:
                LogDemo.v(TAG,"Action_pointer_down, index = " + index);
                break;
            case MotionEventCompat.ACTION_POINTER_UP:
                LogDemo.v(TAG,"action_pointer_up, index = " + index);
                final int tmpId = event.getPointerId(index);
                if(tmpId == mActionID){
                    final int newPointerIndex = index == 0 ? 1 : 0;
                    mActionID = event.getPointerId(newPointerIndex);
                }
                break;
            case MotionEvent.ACTION_UP:
                LogDemo.v(TAG,"action_up, index = " + index);
                mActionID = INVALID_POINTER_ID;
                break;
            case MotionEvent.ACTION_CANCEL:
                LogDemo.v(TAG,"action_cancel, index = " + index);
                mActionID = INVALID_POINTER_ID;
                break;
        }
        return true;
    }
}
